package org.inspiration.dudu.web.controller.content;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.pojo.Result;
import org.inspiration.dudu.content.model.dto.AnswerDTO;
import org.inspiration.dudu.content.model.vo.AnswerVO;
import org.inspiration.dudu.web.annotation.Loggable;
import org.inspiration.dudu.web.service.content.AnswerFacadeService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("answers")
@Tag(name = "回答接口", description = "提供与回答相关的操作接口")
@RequiredArgsConstructor
@Loggable
public class AnswerController {
    private final AnswerFacadeService answerFacadeService;

    @PostMapping
    @Operation(summary = "上传回答", description = "接收用户提交的回答并返回其唯一标识符")
    public Result<String> uploadAnswer(@RequestBody AnswerDTO answerDTO) {
        String uuid = answerFacadeService.uploadAnswer(answerDTO);
        // 返回生成的唯一标识符
        return Result.success(uuid);
    }

    @Operation(summary = "根据问题ID以升序分页获取所有回答", description = "ASC分页查询questionId下的所有回答")
    @GetMapping("/asc")
    public Result<List<AnswerVO>> getAnswersByQuestionIdOrderByASC(
            @RequestParam("questionId") String questionId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        List<AnswerVO> list = answerFacadeService.getAnswersByQuestionIdOrderByASC(questionId, pageNum, pageSize);
        return Result.success(list);
    }

    @Operation(summary = "根据问题ID以降序分页获取所有回答", description = "DESC分页查询questionId下的所有回答")
    @GetMapping("/desc")
    public Result<List<AnswerVO>> getAnswersByQuestionIdOrderByDESC(
            @RequestParam("questionId") String questionId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        List<AnswerVO> list = answerFacadeService.getAnswersByQuestionIdOrderByDESC(questionId, pageNum, pageSize);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取单个回答", description = "根据回答ID获取具体的回答信息")
    public Result<AnswerVO> getAnswerById(@PathVariable("id") String id) {
        AnswerVO answerVO = answerFacadeService.getAnswerById(id);
        return Result.success(answerVO);
    }

    @Operation(summary = "获取用户最近回答")
    @GetMapping("/user")
    public Result<List<Map<String, Object>>> getAnswersByUserIdOrderByDESC(
            @RequestParam("userId") Long userId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize) {
        List<Map<String, Object>> list = answerFacadeService.getAnswersByUserIdOrderByDESC(userId, pageNum, pageSize);
        return Result.success(list);
    }
}
